package com.xyht.sca_s.student_manage_system.modules.assetConsumable.constant;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xyht.sca_s.student_manage_system.modules.asset.entity.SmsAssetCategoryManagement;
import com.xyht.sca_s.student_manage_system.modules.asset.mapper.SmsAssetCategoryManagementMapper;
import com.xyht.sca_s.student_manage_system.modules.assetConsumable.entity.SmsAssetConsumableCategoryManagement;
import com.xyht.sca_s.student_manage_system.modules.assetConsumable.mapper.SmsAssetConsumableCategoryManagementMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import static com.xyht.sca_s.student_manage_system.modules.asset.constant.CategoryDeleteFlagConstant.CATEGORY_DELETE_FLAG_FALSE;

@Component
public class AssetConsumableCategoryInit {

    @Autowired
    private SmsAssetConsumableCategoryManagementMapper smsAssetConsumableCategoryManagementMapper;

    private String[] assetArray = {"办公用品", "日常生活用品", "电子产品"};

    @Bean(value = "consumableCategoryInit")
    public void categoryInit() {
        Integer categoryCount =
                smsAssetConsumableCategoryManagementMapper.selectCount(new QueryWrapper<SmsAssetConsumableCategoryManagement>()
                        .lambda()
                        .eq(SmsAssetConsumableCategoryManagement::getParentId, 0));
        if (categoryCount == 0){
            int id = 1;
            for (String assetName:assetArray) {
                SmsAssetConsumableCategoryManagement smsAssetCategoryManagement = new SmsAssetConsumableCategoryManagement();
                smsAssetCategoryManagement.setCategoryName(assetName);
                smsAssetCategoryManagement.setParentId("0");
                smsAssetCategoryManagement.setChildNum(0);
                smsAssetCategoryManagement.setRootParentId(id+"");
                smsAssetCategoryManagement.setId(id+"");
                smsAssetCategoryManagement.setIsCanDelete(CATEGORY_DELETE_FLAG_FALSE);

                smsAssetConsumableCategoryManagementMapper.insert(smsAssetCategoryManagement);
                id++;
            }
        }
    }
}
